package com.feng.demo.jakartacommons.mina.codec;

import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.filter.codec.demux.MessageEncoder;


public class InfoMessageEncoder implements MessageEncoder<InfoXml> {
	private static Logger logger = Logger.getLogger(InfoMessageEncoder.class);
	private Charset charset;

	public InfoMessageEncoder(Charset charset) {
		this.charset = charset;
	}

	@Override
	public void encode(IoSession session, InfoXml ix, ProtocolEncoderOutput out) throws Exception {
		IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
		buf.putShort(ix.getTag());
		buf.putInt(ix.getLen(charset));
		buf.putString(ix.getXml(), charset.newEncoder());
		buf.flip();
		logger.info("编码" + buf.toString());
		out.write(buf);
	}
}
